package cn.itsource.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.system.domain.Logininfo;
import cn.itsource.system.mapper.LogininfoMapper;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.query.UserQuery;
import cn.itsource.user.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：用户User
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(User user) {
        //添加数据到tb_user中
        //1.生成随机的盐值
        String salt = RandomUtil.randomString(32);
        //2.使用工具类加密加盐  Md5()
        String md5Pwd = SecureUtil.md5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);


        //添加数据到tb_logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);//根据属性同名原则拷贝
        logininfo.setUsername(user.getUserName());
        logininfo.setType(1);
        logininfoMapper.add(logininfo);

        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }

    @Transactional
    @Override
    public void update(User user) {
        User oldUser = userMapper.findOne(user.getId());
        //修改user信息：前端没有传递：salt，password，logininfo_id
        userMapper.update(user);

        //修改logininfo
        Logininfo logininfo = new Logininfo();
        //id注意: id,phone,email
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setId(oldUser.getLogininfoId());
        logininfo.setUsername(user.getUserName());
        logininfoMapper.update(logininfo);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        User user = userMapper.findOne(id);
        //删除用户信息
        userMapper.delete(id);
        //删除登录信息
        logininfoMapper.delete(user.getLogininfoId());
    }

    @Transactional
    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public PageInfo<User> queryPage(UserQuery userQuery) {
        //开启分页
        PageHelper.startPage(userQuery.getCurrentPage(),userQuery.getPageSize());
        //1.首先会查询总条数
        //2.会根据方言或驱动名进行分页查询数据 - mysql - limit
        //3.会自动计算limit的第一个参数

        List<User> list = userMapper.queryPage(userQuery);
        //调用PageInfo的有参会计算其他参数
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        return pageInfo;
    }
}
